require 'if_it_werent_4_AR/associations'
require 'if_it_werent_4_AR/initialization'
require 'if_it_werent_4_AR/file_op'
require 'if_it_werent_4_AR/storage_crud'
require 'if_it_werent_4_AR/search'
require 'if_it_werent_4_AR/serialization'
require 'if_it_werent_4_AR/validations'

module IfItWerent4AR
  VALID_DATA_TYPES = %w(
    integer
    text
  )

  class Base
    include StorageCRUD
    include Serialization
    extend Associations
    extend Search

    class << self
      attr_accessor :attributes_with_types
      attr_reader :belongs_to, :has_many

      def attributes
        attributes_with_types.keys
      end

      def primary_key
        attributes.first
      end
    end

    def initialize(attrs = {})
      # if attrs.present?
      attrs.stringify_keys!
      uknown_attrs = attrs.keys - self.class.attributes
      raise "Unknown attributes #{uknown_attrs}" if uknown_attrs.present?
      # end
      assign_attributes(attrs)
    end

    def assign_attributes(attrs)
      self.class.attributes.each { |attr| public_send("#{attr}=", attrs[attr]) }
    end
  end
end
